# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=poppler
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"))
pkgver=23.08.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://poppler.freedesktop.org/"
license=('spdx:GPL-2.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-base")
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-poppler-data"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://poppler.freedesktop.org/${_realname}-${pkgver}.tar.xz{,.sig}
        test::git+https://gitlab.freedesktop.org/poppler/test.git#commit=920c89f8f43bdfe8966c8e397e7f67f5302e9435
        '0001-nopython-generate-enums.patch')
sha256sums=('4a4bf7fc903b9f1a2ab7d04b7c5d8220db9bc6261cc73fdb9a826dc272f49aa8'
            'SKIP'
            'SKIP'
            '315356263b5ebee5431a2e079ac76492a8919a04a798f6c6d53c64f44b433db6')
validpgpkeys=('CA262C6C83DE4D2FB28A332A3A6A4DB839EAA6D7') # "Albert Astals Cid <aacid@kde.org>"

prepare() {
  cd ${_realname}-${pkgver}

  patch -p1 -i "${srcdir}/0001-nopython-generate-enums.patch"
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  CXXFLAGS+=" -Wno-deprecated-declarations -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake.exe" \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
    -DSPLASH_CMYK=ON \
    -DENABLE_GLIB=ON \
    -DENABLE_GOBJECT_INTROSPECTION=ON \
    -DENABLE_CPP=ON \
    -DENABLE_DCTDECODER=libjpeg \
    -DENABLE_LIBOPENJPEG=openjpeg2 \
    -DENABLE_CMS=lcms2 \
    -DENABLE_NSS3=ON \
    -DENABLE_UTILS=ON \
    -DENABLE_LIBCURL=ON \
    -DENABLE_ZLIB=ON \
    -DENABLE_ZLIB_UNCOMPRESS=OFF \
    -DENABLE_GTK_DOC=OFF \
    -DENABLE_RELOCATABLE=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd ${srcdir}/build-${MSYSTEM}

  # Prepend build directory with PATH to get DLL file
  PATH=$PWD:$PWD/cpp:$PWD/qt5/src:$PWD/qt6/src:$PATH ${MINGW_PREFIX}/bin/ctest.exe . || warning "Tests failed"
}

package_poppler() {
  pkgdesc='PDF rendering library based on xpdf 3.0 (mingw-w64)'
  optdepends=("${MINGW_PACKAGE_PREFIX}-glib2: libpoppler-glib")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  # cleanup for splitted build
  rm -vrf "${pkgdir}${MINGW_PREFIX}"/include/poppler/{qt5,qt6}
  rm -vf "${pkgdir}${MINGW_PREFIX}"/bin/libpoppler-{qt5,qt6}*
  rm -vf "${pkgdir}${MINGW_PREFIX}"/lib/libpoppler-{qt5,qt6}*
  rm -vf "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/poppler-{qt5,qt6}.pc
}

package_poppler-qt5() {
  pkgdesc='Poppler Qt5 bindings (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-poppler=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-qt5-base")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target qt5/install
  install -Dm644 poppler-qt5.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/poppler-qt5.pc"
}

package_poppler-qt6() {
  pkgdesc='Poppler Qt6 bindings (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-poppler=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-qt6-base")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target qt6/install
  install -Dm644 poppler-qt6.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/poppler-qt6.pc"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
